package keter.combofs.persist.impl;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import keter.combofs.model.ComboFile;
import keter.combofs.persist.ComboFileDao;

import java.io.FileNotFoundException;
import java.util.Date;
import java.util.Map;

/**
 * Created by gulx@neusoft.com on 2019/11/11.
 */
public class ComboFileDaoJDBCImpl implements ComboFileDao {
    @Override
    public void save(ComboFile comboFile) {
        Record r = new Record();
        r.set("id",comboFile.getId());
        r.set("name",comboFile.getName());
        r.set("path",comboFile.getPath());
        r.set("meta", JsonKit.toJson(comboFile.getMeta()));
        r.set("created",new Date());
        Db.save("COMBOFILE",r);
    }

    @Override
    public ComboFile find(Long id) throws FileNotFoundException {
        Record r = Db.findFirst("select * from combofile c where id=?", id);
        if(r==null){
            throw new FileNotFoundException("数据库中未找到ID=["+id+"]的记录");
        }
        Map meta = JSONObject.parseObject(r.getStr("meta"), Map.class);
        return ComboFile.build(id,r.getStr("name"),r.getStr("path"),meta,null);
    }
}
